package com.baidu.health.service.impl;

import com.baidu.health.mapper.TbPlatformMapper;
import com.baidu.health.mapper.TbPlatformRoleMapper;
import com.baidu.health.pojo.ResultResponse;
import com.baidu.health.pojo.TbPlatform;
import com.baidu.health.pojo.TbPlatformRole;
import com.baidu.health.service.TbPlatformService;
import com.baidu.health.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 */
@Service
public class TbPlatformServiceImpl extends ServiceImpl<TbPlatformMapper, TbPlatform>
    implements TbPlatformService{
    @Autowired
    private TbPlatformMapper platformMapper;
    @Autowired
    private TbPlatformRoleMapper tbPlatformRoleMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ResultResponse loginPlatformname(TbPlatform tbPlatform) {
        String platformName = tbPlatform.getPlatformName();
        String platformPassword = tbPlatform.getPlatformPassword();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("platform_name",platformName);
        TbPlatform tbPlatform1 = platformMapper.selectOne(queryWrapper);
        if(tbPlatform1==null){
            return ResultResponse.FAILED(500,"内部用户名不正确");
        }
        if(!tbPlatform1.equals(platformPassword)){
            return ResultResponse.FAILED(500,"密码不正确");
        }
        String token = (String) JwtUtils.getToken();
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("platformid",tbPlatform1.getPlatformId());
        map.put("platformname",tbPlatform1.getPlatformName());
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse loginPlatformphone(TbPlatform tbPlatform) {
        String platformPhone = tbPlatform.getPlatformPhone();
        String code = tbPlatform.getCode();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("platform_phone",platformPhone);
        TbPlatform tbPlatform1 = platformMapper.selectOne(queryWrapper);
        String phone = (String) redisTemplate.opsForValue().get(platformPhone);
        if(phone==null){
            return ResultResponse.FAILED(500,"验证码已失效");
        }
        if(tbPlatform1==null){
            return ResultResponse.FAILED(500,"用户名不对");
        }
        if(!phone.equals(code)){
            return ResultResponse.FAILED(500,"验证码不正确");
        }
        String token = (String) JwtUtils.getToken();
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("platformid",tbPlatform1.getPlatformId());
        map.put("platformname",tbPlatform1.getPlatformName());
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse platformselect() {
        List<TbPlatform> list = platformMapper.selectList(null);
        List<TbPlatformRole> list1 = tbPlatformRoleMapper.selectList(null);
        for (int i = 0; i < list.size(); i++) {
            for (int i1 = 0; i1 < list1.size(); i1++) {
                if(list1.get(i1).getPlatformId().equals(list.get(i).getPlatformId())){
                    list.get(i).setRole(list1.get(i1).getRoleId());
                }
            }
        }
        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse platformInsert(TbPlatform tbPlatform) {
        tbPlatform.setPlatformPassword("123456");
        tbPlatform.setCreateTime(new Date());
        platformMapper.insert(tbPlatform);
        System.out.println(tbPlatform);
        Integer platformId = tbPlatform.getPlatformId();
        TbPlatformRole tbPlatformRole=new TbPlatformRole();
        tbPlatformRole.setPlatformId(platformId);
        tbPlatformRole.setRoleId(tbPlatform.getRole());
        tbPlatformRoleMapper.insert(tbPlatformRole);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse platformupdate(Integer platformId) {
        platformMapper.deleteById(platformId);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("platform_id",platformId);
        TbPlatformRole tbPlatformRole = tbPlatformRoleMapper.selectOne(queryWrapper);
        tbPlatformRoleMapper.deleteById(tbPlatformRole.getId());
        return ResultResponse.SUCCESS("成功");
    }

    @Override
    public ResultResponse customerSelect() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("role_id",3);
        List<TbPlatformRole> list = tbPlatformRoleMapper.selectList(queryWrapper);
        List<TbPlatform> list1 = platformMapper.selectList(null);
        List<TbPlatform> list2 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            for (int i1 = 0; i1 < list1.size(); i1++) {
                System.out.println(list1.get(i1).getPlatformId());
                if(list.get(i).getPlatformId().equals(list1.get(i1).getPlatformId())){
                    TbPlatform tbPlatform=new TbPlatform();
                    tbPlatform=list1.get(i1);
                    tbPlatform.setRole(list.get(i).getRoleId());
                    list2.add(tbPlatform);
                }
            }
        }
        return ResultResponse.SUCCESS(list2);
    }

    @Override
    public ResultResponse customerInsert(TbPlatform platform) {
        platform.setPlatformImage("http://create-file.oss-cn-beijing.aliyuncs.com/test/772a395a-252b-4eac-b85a-ba9a60e6d86e.jpg");
        platform.setCreateTime(new Date());
        platformMapper.insert(platform);
        TbPlatformRole tbPlatformRole=new TbPlatformRole();
        tbPlatformRole.setRoleId(3);
        tbPlatformRole.setPlatformId(platform.getPlatformId());
        tbPlatformRole.setCreateTime(new Date());
        tbPlatformRoleMapper.insert(tbPlatformRole);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse customerDelete(Integer platformId) {
        platformMapper.deleteById(platformId);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("platform_id",platformId);
        TbPlatformRole tbPlatformRole = tbPlatformRoleMapper.selectOne(queryWrapper);
        tbPlatformRoleMapper.deleteById(tbPlatformRole.getId());
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse customerUpddate(TbPlatform platform) {
        platformMapper.updateById(platform);
        return ResultResponse.SUCCESS();
    }
}




